//
//  BLHotelCustomTableViewCell.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotelCustomTableViewCell.h"
#import "STAutoAddView.h"
#import "BLTextFieldControl.h"
@interface BLHotelCustomTableViewCell()<UITextFieldDelegate>
@property(nonatomic, strong) UIImageView                  *iconImageView;
@property(nonatomic, strong) STLabel                     *titleLable;
//质量标准
@property(nonatomic, strong) STButton                     *lowButton;
@property(nonatomic, strong) STButton                     *normalButton;
@property(nonatomic, strong) STButton                     *highButton;



@property(nonatomic, strong) STLabel                     *priceLable;
@property(nonatomic, strong) STLabel                     *gradeLable;//赠送盖币
@property(nonatomic, strong) STLabel                     *scroeLable;//积分购买

@property(nonatomic, strong) NSArray                     *buttonsArray;
@end

@implementation BLHotelCustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configSubView];
        [self addNotifacations];
    }
    return self;
}
+ (CGFloat)cellHeight{
    return 180 + 46 + 23;
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFiledDidChangeNotifacation:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    

    
}
- (void)textFiledDidChangeNotifacation:(NSNotification*)notifacation{
    if (notifacation.object == self.sizehoricalTextfield.textFiled) {
        if (self.model) {
            self.model.chang = self.sizehoricalTextfield.textFiled.text;
        }
    }
    if (notifacation.object == self.sizeVerticalTextfield) {
        if (self.model) {
            self.model.kuan = self.sizeVerticalTextfield.text;
        }
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endEditing:YES];
    });
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.sizehoricalTextfield.textFiled) {
        NSString * chang = self.sizehoricalTextfield.textFiled.text;
         NSString * kuan = self.sizeVerticalTextfield.text;
        if (chang.length && kuan.length) {
            if (self.changKuanDidEndEditing) {
                self.changKuanDidEndEditing(chang, kuan);
            }
        }
    }
    if (textField == self.sizeVerticalTextfield) {
        NSString * chang = self.sizehoricalTextfield.textFiled.text;
        NSString * kuan = self.sizeVerticalTextfield.text;
        if (chang.length && kuan.length) {
            if (self.changKuanDidEndEditing) {
                self.changKuanDidEndEditing(chang, kuan);
            }
        }
    }
}
#pragma mark --subView
- (void)configSubView{
    

    CGFloat lableHeight = 23;
    self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 , 10, 117, 117)];
    self.iconImageView.image = [UIImage imageNamed:@"个人头像"];
    self.iconImageView.top = 20;
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.iconImageView.clipsToBounds = YES;
    [self.contentView addSubview:self.iconImageView];
    
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.iconImageView.right + 10, self.iconImageView.top-4, UIScreenWidth - 20 - self.iconImageView.right, 2  * lableHeight)
                                                     text:@"床单"
                                           textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:self.titleLable];
    
    STLabel * zhiliangLable = [[STLabel alloc] initWithFrame:CGRectMake(self.iconImageView.right + 10,self.titleLable.bottom, 70
                                                                        , lableHeight)
                                                        text:@"质量标准:"
                                                   textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:zhiliangLable];
    
    self.lowButton = [self defultButtonWithTitle:@"60*40" tag:10001];
    self.lowButton.left = zhiliangLable.right + 3;
    [self.contentView  addSubview:self.lowButton];
    
    self.normalButton = [self defultButtonWithTitle:@"40*40" tag:10001];
    self.normalButton.left = self.lowButton.right + 5;
    [self.contentView  addSubview:self.normalButton];
    
    self.highButton = [self defultButtonWithTitle:@"60*60" tag:10001];
    self.highButton.left = self.normalButton.right + 5;
    [self.contentView  addSubview:self.highButton];
    //自定义尺寸
    self.sizehoricalTextfield = [[BLTextFieldControl alloc] initWithLineFrame:CGRectMake(_titleLable.left, zhiliangLable.bottom, 130, lableHeight - 1) leftTitle:@"制定尺寸:" rightTitle:@"*"];
    self.sizehoricalTextfield.lineView.backgroundColor = BL_lineColor;
    self.sizehoricalTextfield.textFiled.font = [UIFont systemFontOfSize:13];
    self.sizehoricalTextfield.textFiled.textColor = BL_secendTextColor;
    self.sizehoricalTextfield.textFiled.placeholder = @"长(m)";
    self.sizehoricalTextfield.textFiled.delegate = self;
    self.sizehoricalTextfield.textFiled.keyboardType = UIKeyboardTypeDecimalPad ;
    [self.contentView  addSubview:self.sizehoricalTextfield];
    
    self.sizeVerticalTextfield = [[UITextField alloc] initWithFrame:CGRectMake(self.sizehoricalTextfield.right, zhiliangLable.bottom, 60, lableHeight - 1)];
    self.sizeVerticalTextfield.keyboardType = UIKeyboardTypeDecimalPad;
    [self.sizeVerticalTextfield st_showBottomLine];
    self.sizeVerticalTextfield.placeholder = @"宽(m)";
    self.sizeVerticalTextfield.delegate = self;
    self.sizeVerticalTextfield.font = [UIFont systemFontOfSize:13];
    self.sizeVerticalTextfield.textColor = BL_secendTextColor;
    [self.contentView  addSubview:self.sizeVerticalTextfield];
    
    self.priceLable = [[STLabel alloc] initWithFrame:CGRectMake(self.titleLable.left, _sizeVerticalTextfield.bottom, self.titleLable.width, lableHeight)
                                                text:@"价格:￥80.00"
                                                textColor:BL_redColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:self.priceLable];
    self.priceLable.attributedText = [self.priceLable.text st_convertAttributeStringWithKeyWord:@"价格:" attributes:@{NSForegroundColorAttributeName:BL_firstTextColor}];
    
    self.scroeLable = [[STLabel alloc] initWithFrame:CGRectMake(self.titleLable.left, _priceLable.bottom, self.titleLable.width, lableHeight)
                                                text:@"￥200 + 20 积分"
                                           textColor:BL_firstTextColor
                                                font:14
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:self.scroeLable];
    
    self.gradeLable = [[STLabel alloc] initWithFrame:CGRectMake(self.titleLable.left, _scroeLable.bottom, self.titleLable.width, lableHeight)
                                                text:@"将赠送盖币:0"
                                           textColor:BL_firstTextColor
                                                font:14
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:self.gradeLable];
    
    STLabel * shuliangLable = [[STLabel alloc] initWithFrame:CGRectMake(self.iconImageView.right + 10,self.gradeLable.bottom, 40, lableHeight)
                                                        text:@"数量:"
                                                   textColor:BL_firstTextColor
                                                        font:14
                                                 isSizetoFit:NO
                                               textAlignment:NSTextAlignmentLeft];
    [self.contentView  addSubview:shuliangLable];
    self.autoAddView = [[STAutoAddView alloc] initWithFrame:CGRectMake(shuliangLable.right+3, shuliangLable.top, lableHeight * 3, lableHeight)];
    [self.contentView  addSubview:self.autoAddView];
    
    
    self.addButton = [[STButton alloc] initWithFrame:CGRectMake(0, self.autoAddView.bottom+ 10, 80, 30)
                                                     title:@"加入购物车"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:14
                                              cornerRadius:5
                                           backgroundColor:BL_BlueBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    self.addButton.right = UIScreenWidth - 10;
    self.addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.contentView  addSubview:self.addButton];
    
    
    self.chosedButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                  title:nil
                                             titleColor:nil
                                              titleFont:0
                                           cornerRadius:0
                                        backgroundColor:nil
                                        backgroundImage:nil
                                                  image:[UIImage imageNamed:@"购物未选中"]];
    
    [self.chosedButton setImage:[UIImage imageNamed:@"购物选中"] forState:UIControlStateSelected];
    self.chosedButton.centerY = self.addButton.centerY;
    self.chosedButton.right = self.addButton.left - 20;
    self.chosedButton.contentHorizontalAlignment= UIControlContentHorizontalAlignmentCenter;
    [self.contentView  addSubview:self.chosedButton];
    
    
    self.buttonsArray = @[self.lowButton,self.highButton,self.normalButton];
}
- (STButton*)defultButtonWithTitle:(NSString*)title tag:(NSInteger)tag{
    __weak typeof(self) weakSelf =  self;
     CGFloat lableHeight = 23;
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(3, self.titleLable.bottom, 45, lableHeight)
                                               title:@"60*40"
                                          titleColor:BL_secendTextColor
                                           titleFont:13
                                        cornerRadius:4
                                     backgroundColor:BL_backgroundColor
                                     backgroundImage:nil
                                               image:nil];
    button.selected = NO;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedQulityButton:sender];
    }];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.tag = tag;
    return button;
}
- (void)onSlectedQulityButton:(UIButton*)sender{

    [self makeButttobSelected:sender];
    if (self.model) {
        NSInteger num = [self.buttonsArray indexOfObject:sender];
        self.model.chosedMassModel = self.model.mass[num];
    }
    if (self.qulityButtonDidChanged) {
        self.qulityButtonDidChanged(self.model.chosedMassModel);
    }
}
- (void)makeButttobSelected:(UIButton*)sender{
    for (UIButton * button in self.buttonsArray) {
        button.selected = NO;
        sender.selected = YES;
        if (button.selected) {
            button.backgroundColor = BL_BlueBackGroundColor;
            
        }else{
            button.backgroundColor = BL_backgroundColor;
        }
    }
}
- (void)setModel:(BLCustomModel *)model{
    _model = model;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.img_url]];
    
    self.titleLable.text = model.name;
    
    [self.lowButton setTitle:model.mass.firstObject.name forState:UIControlStateNormal] ;
    
    [self.normalButton setTitle:model.mass[1].name forState:UIControlStateNormal] ;
    
    [self.highButton setTitle:model.mass.lastObject.name forState:UIControlStateNormal] ;
    
    NSInteger num = [model.mass indexOfObject:model.chosedMassModel];
    
    [self makeButttobSelected:self.buttonsArray[num]];
    

    self.chosedButton.selected = model.selected;
    self.autoAddView.titleLable.text = model.num.description;
    
    self.sizehoricalTextfield.textFiled.text = model.chang;
    self.sizeVerticalTextfield.text = model.kuan;
    
    NSString * price = model.member_money.description;
    if (!model.chang.length &&  !model.kuan.length) {
        price = @"0";
    }
    CGFloat dalaPrice = [price floatValue];
    self.priceLable.text = [NSString stringWithFormat:@"会员价:￥%0.2f",dalaPrice];
    self.priceLable.attributedText = [self.priceLable.text st_convertAttributeStringWithKeyWord:@"会员价:" attributes:@{NSForegroundColorAttributeName:BL_firstTextColor}];
    
    
    NSString * score_money = model.score_money?model.score_money.description:@"0";
    NSString * score = model.score?model.score.description:@"0";
    NSString * grade = model.grade?model.grade.description:@"0";
    self.scroeLable.text = [NSString stringWithFormat:@"积分价格:￥%@+%@积分",score_money,score];
    
     self.gradeLable.text = [NSString stringWithFormat:@"会员价购买将赠送%@个盖币",grade];
    
}
@end
